package com.zmj.sy.mom.srv.aps.utils.mybatisplus;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;


public class WrapperUtil {
    public static void fixWrapper(QueryWrapper<?> query, String key, String compareType, Object value) {
        if (StringUtils.isBlank(compareType)) {
            query.like(key, value);
        } else {
            switch (compareType) {
                case "eq":
                    query.eq(key, value);
                    break;
                case "ne":
                    query.ne(key, value);
                    break;
                case "lt":
                    query.lt(key, value);
                    break;
                case "le":
                    query.le(key, value);
                    break;
                case "gt":
                    query.gt(key, value);
                    break;
                case "ge":
                    query.ge(key, value);
                    break;
                case "like":
                    query.like(key, value);
                    break;
                case "notLike":
                    query.notLike(key, value);
                    break;
                case "startWith":
                    query.likeLeft(key, value);
                    break;
                case "notStartWith":
                    query.notLike(key, value);
                    break;
                case "endWith":
                    query.likeRight(key, value);
                    break;
                case "notEndWith":
                    query.notLike(key, value);
                    break;
                case "isNull":
                    query.isNull(key);
                    break;
                case "isNotNull":
                    query.isNotNull(key);
                    break;
                case "isEmpty":
                    query.eq(key, "");
                    break;
                case "isNotEmpty":
                    query.ne(key, "");
                    break;
                case "in":
                    query.in(key, value);
                    break;
                case "notIn":
                    query.notIn(key, value);
                    break;
                default:
                    query.like(key, value);
                    break;
            }
        }
    }

    public static void fixWrapper(QueryWrapper<?> query, boolean condition, String key, String compareType, Object value) {
        if(condition){
            fixWrapper(query, key, compareType, value);
        }
    }


    public static boolean fixWrapper(String compareType, Integer x, Integer y) {
        boolean flag = false;
        if ("eq".equals(compareType)) {
            flag = x.equals(y);
        }
        if ("ge".equals(compareType)) {
            flag = Integer.compare(x, y) >= 0;
        }
        if ("gt".equals(compareType)) {
            flag = Integer.compare(x, y) > 0;
        }
        if ("le".equals(compareType)) {
            flag = Integer.compare(x, y) < 0;
        }
        if ("lt".equals(compareType)) {
            flag = Integer.compare(x, y) <= 0;
        }
        return flag;
    }


    public static boolean fixWrapper(String compareType, Double x, Double y) {
        boolean flag = false;
        if ("eq".equals(compareType)) {
            flag = x.equals(y);
        }
        if ("ge".equals(compareType)) {
            flag = Double.compare(x, y) >= 0;
        }
        if ("gt".equals(compareType)) {
            flag = Double.compare(x, y) > 0;
        }
        if ("le".equals(compareType)) {
            flag = Double.compare(x, y) < 0;
        }
        if ("lt".equals(compareType)) {
            flag = Double.compare(x, y) <= 0;
        }
        return flag;
    }
}
